package com.xin.ruanjian.omsystem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.bean.DishBean;
import com.xin.ruanjian.omsystem.model.ShopCarModel;
import com.xin.ruanjian.omsystem.utils.ShopCarUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.OrderedRealmCollection;
import io.realm.RealmRecyclerViewAdapter;

/**
 * Created by local123 on 2018/2/23.
 */

public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.ViewHolder> {
    private Context context;
    private List<ShopCarModel> mData;
    private onItemClickListener listener;

    public ShopCarAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<ShopCarModel> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    public void setListener(onItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public ShopCarAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shop_car_window_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ShopCarAdapter.ViewHolder holder, final int position) {
        holder.tvItemShopCarWindowName.setText(mData.get(position).getdName());
        holder.tvItemShopCarWindowNum.setText(mData.get(position).getdNum() + "");
        holder.ivItemShopCarWindowAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int i = mData.get(position).getdNum() + 1;
                    listener.onClick(mData.get(position), i);
                    notifyItemChanged(position);
                }
            }
        });
        holder.ivItemShopCarWindowSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int i = mData.get(position).getdNum() - 1;
                    listener.onClick(mData.get(position), i);
                    if (i <= 0) {
                        mData.remove(position);
                    }
                    notifyItemChanged(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_item_shop_car_window_name)
        TextView tvItemShopCarWindowName;
        @BindView(R.id.iv_item_shop_car_window_sub)
        ImageView ivItemShopCarWindowSub;
        @BindView(R.id.tv_item_shop_car_window_num)
        TextView tvItemShopCarWindowNum;
        @BindView(R.id.iv_item_shop_car_window_add)
        ImageView ivItemShopCarWindowAdd;
        @BindView(R.id.ll_item_shop_car_window)
        LinearLayout llItemShopCarWindow;
        @BindView(R.id.tv_item_shop_car_window_price)
        TextView tvItemShopCarWindowPrice;
        @BindView(R.id.tv_item_shop_car_window_guige)
        TextView tvItemShopCarWindowGuige;
        @BindView(R.id.rl_item_shop_car_window)
        RelativeLayout rlItemShopCarWindow;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface onItemClickListener {
        void onClick(ShopCarModel model, int num);
    }
}
